from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
import dbutil
import pymainwindow

ve, _ = loadUiType('ininventorymation.ui')

class ininventorymationApp(QMainWindow,ve):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.confirminsertButton.clicked.connect(lambda:self.add_inventoryinformation())
        self.menu.triggered[QAction].connect(self.menuTriggered)

    def menuTriggered(self, q):
        if (q.text() == "返回上一页"):
            self.mainwindow_app = pymainwindow.MWApp()
            self.close()
            self.mainwindow_app.show()

        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return

    # 数据库处理
    def add_inventoryinformation(self):
        #获取连接
        conn = dbutil.get_conn()
        #获取cursor
        cur = conn.cursor()
        #SQL语句
        sql = "insert into inventory(商品名称,现存数量,数量单位,存放仓库,进货总数量,销售总数量,库管员) values(%s,%s,%s,%s,%s,%s,%s)"
        goodsname = self.goodsnameEdit.text()
        quantity = self.quantityEdit.text()
        quantityunit = self.quantityunitEdit.text()
        warehouse = self.warehouseEdit.text()
        purchase = self.purchaseEdit.text()
        sell = self.sellEdit.text()
        staff = self.staffEdit.text()
        if (goodsname=='' or quantity=='' or quantityunit=='' or warehouse=='' or purchase=='' or sell=='' or staff==''):
            QMessageBox.warning(self,"警告", "不能为空！", QMessageBox.Yes)
        else:
            sql1 = 'select * from inventory where 商品名称=%s'
            cur.execute(sql1, (goodsname,))
            data = cur.fetchone()
            if data:
                QMessageBox.warning(self, "警告", "该商品库存信息已存在", QMessageBox.Yes)
                self.statusBar().showMessage('请直接修改该商品库存信息!')
                self.goodsnameEdit.clear()
                self.quantityEdit.clear()
                self.quantityunitEdit.clear()
                self.warehouseEdit.clear()
                self.purchaseEdit.clear()
                self.sellEdit.clear()
                self.staffEdit.clear()
            else:
                warning = QMessageBox.warning(self,"提示","您确定添加此条库存信息吗？", QMessageBox.Yes | QMessageBox.No)
                if warning == QMessageBox.Yes:
                    #执行语句
                    cur.execute(sql, (goodsname, quantity, quantityunit, warehouse, purchase, sell, staff ))
                    #insert,update,delete必须显示提交
                    conn.commit()
                    #关闭资源
                    dbutil.close_conn(conn, cur)
                    #消息提示
                    QMessageBox.information(self, "提示","添加成功!", QMessageBox.Yes)
                    self.goodsnameEdit.clear()
                    self.quantityEdit.clear()
                    self.quantityunitEdit.clear()
                    self.warehouseEdit.clear()
                    self.purchaseEdit.clear()
                    self.sellEdit.clear()
                    self.staffEdit.clear()


def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = ininventorymationApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication


if __name__ == '__main__':
    main()